var express = require('express');
var path = require('path');

var config = require('./lib/config');
var logger = require('./service/logging').logger;
var methodOverride = require('method-override');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var multer = require('multer');
var errorHandler = require('errorhandler');
var midware_login_admin = require('./routes/midware').midware_login_admin;
var security_interceptor = require('./routes/security_interceptor').request_decide; //api级资源权限验证
var midware_crossdomain = require('./routes/midware').midware_crossdomain;
var midware_login_wechat = require("./routes/midware").midware_login_wechat;
var midware_login_wechat_rest = require("./routes/midware").midware_login_wechat_rest;
var midware_login_wechat_binduser = require("./routes/midware").midware_login_wechat_binduser;
var compression = require('compression');
var app = express();

process.env.NODE_ENV = "development";


var nunjucks = require("nunjucks");
// var midware_login_fw = require('./routes/midware').midware_login_fw;
// var midware_login_rest = require('./routes/midware').midware_login_rest;
nunjucks.configure('views', {
  autoescape: true,
  express: app
});

//设置 Session
app.use(session({
  store: new RedisStore({
    // host: config.redis.remote.host,
    // port: config.redis.remote.port,
    host: config.redis.host,
    port: config.redis.port,
    db: 2
  }),
  cookie: {maxAge: 60000 * 20 * 6}, //两小时 
  secret: 'cms@1234',
  resave: true,//是指每次请求都重新设置session cookie，假设你的cookie是10分钟过期，每次请求都会再设置10分钟
  saveUninitialized: true//是指无论有没有session cookie，每次请求都设置个session cookie
}));
app.use(compression());
// app.use(express.compress());
app.set('port', config.listen_port || 80);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

//app.use(logger('dev'));
//app.use(express.logger("PV`:date`:method`:url`:response-time`:res[Content-Length]"));
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(multer());
app.use(express.static(path.join(__dirname, 'public')));
// development only
app.use(midware_crossdomain);
if ('development' == app.get('env')) {
    app.use(errorHandler());
}
app.use(function(err, req, res, next) {
  var error_msg;
  var user = '';
  if (req && req.session && req.session.adminuser) {
      user = req.session.adminuser.displayname
  }
  error_msg = util.inspect({
      type: "Exception",
      path: req.path,
      method: req.method,
      query: req.query,
      body: req.body,
      user: user,
      error: err.message || err.toString(),
      stack: err.stack
  });
  logger.error(error_msg);
  console.error(error_msg);
  res.send(500, {
      error: err.message || err.toString()
  });
});

/*数据字典相关*/
app.all('/dict/list',midware_login_admin,require('./routes/dict').dict_list);
app.all('/dict/list_by_keycode', midware_login_admin, require('./routes/dict').dict_list_by_keycode);
app.all('/dict/tree',midware_login_admin,require('./routes/dict').dict_tree);
app.all('/dict/check',midware_login_admin,require('./routes/dict').dict_check);
app.all('/dict/add',midware_login_admin,require('./routes/dict').dict_add);
app.all('/dict/edit',midware_login_admin,require('./routes/dict').dict_edit);
app.all('/dict/delete',midware_login_admin,require('./routes/dict').dict_del);
app.all('/dict/detail',midware_login_admin,require('./routes/dict').dict_detail);
/*管理员操作*/
app.all('/admin_user/login',require('./routes/admin_user').login_ownpwd_admin);
app.all('/admin_user/logout',require('./routes/admin_user').adminuser_logout);
app.all('/admin_user/list',require('./routes/admin_user').adminuser_list);
app.all('/admin_user/add',require('./routes/admin_user').adminuser_add);
app.all('/admin_user/upd',require('./routes/admin_user').adminuser_upd);
app.all('/admin_user/del/:user_id',require('./routes/admin_user').adminuser_del);
app.all('/admin_user/session',midware_login_admin,require('./routes/admin_user').adminuser_session);
app.all('/admin_user/menu',midware_login_admin,require('./routes/admin_user').adminuser_menu);
app.all('/admin_user/user_types', midware_login_admin, require('./routes/admin_user').user_types);
app.all('/', require('./routes/admin_user').login_default);
/*资源相关*/
app.all('/resource/list',midware_login_admin,require('./routes/resource').resource_list);
app.all('/resource/tree',midware_login_admin,require('./routes/resource').resource_tree);
app.all('/resource/edit',midware_login_admin,require('./routes/resource').resource_edit);
app.all('/resource/check',midware_login_admin,require('./routes/resource').resource_check);
app.all('/resource/checktree',midware_login_admin,require('./routes/resource').resource_checktree);
app.all('/resource/del/:resource_id',midware_login_admin,require('./routes/resource').resource_del);
/*角色相关*/
app.all('/role/list',midware_login_admin,require('./routes/role').role_list);
app.all('/role/edit',midware_login_admin,require('./routes/role').role_edit);
app.all('/role/check',midware_login_admin,require('./routes/role').role_check);
app.all('/role/del/:role_id',midware_login_admin,require('./routes/role').role_del); 
app.all('/role/users',midware_login_admin,require('./routes/role').role_users);
app.all('/role/rmUserInRole',midware_login_admin,require('./routes/role').role_removeuser);

//签到相关
app.all('/sign/sign',midware_login_wechat_rest,require('./routes/sign').sign);
app.all('/sign/list_today',midware_login_wechat_rest,require('./routes/sign').sign_list_today);
app.all('/sign/list',midware_login_wechat_rest,require('./routes/sign').sign_list);


// 请假相关
app.all("/leave/ask_leave",midware_login_wechat_rest,require("./routes/leave").leave);
app.all('/leave/list_student',midware_login_wechat_rest,require('./routes/leave').leavelist_student);
app.all('/leave/list_teacher',midware_login_admin,require('./routes/leave').leavelist_teacher);
app.all('/leave/list', midware_login_admin, require('./routes/leave').leave_list);
app.all('/leave/reject', midware_login_admin, require('./routes/leave').leave_reject);
app.all('/leave/approval', midware_login_admin, require('./routes/leave').leave_approval);

// app.all('/user/sign',midware_login_admin,require('./routes/user').sign_new);
app.all('/user/sign_detail',midware_login_admin,require('./routes/user').sign_detail);
app.all('/user/sign_list',midware_login_admin,require('./routes/user').sign_list);
app.all('/user/sign_list_teacher', midware_login_admin, require('./routes/user').sign_list_teacher);


//请假相关
app.all('/user/get_verify',midware_login_admin,require('./routes/user').get_verify);
app.all('/user/leave',midware_login_admin,require('./routes/user').leave);
app.all('/user/leavelist_student',midware_login_admin,require('./routes/user').leavelist_student);
app.all('/user/leavelist_teacher',midware_login_admin,require('./routes/user').leavelist_teacher);
app.all('/user/apply_leave',midware_login_admin,require('./routes/user').apply_leave);
app.all('/user/leavelist_director',midware_login_admin,require('./routes/user').leavelist_director);
app.all('/user/apply_leave_director',midware_login_admin,require('./routes/user').apply_leave_director);
app.all('/user/bind_openid',midware_login_wechat_rest,require('./routes/user').user_openid_bind);

//参数相关
app.all('/config/list',midware_login_admin,require('./routes/param').param_list);
app.all('/config/add', midware_login_admin, require('./routes/param').param_add);
app.all('/config/del/:param_cd', midware_login_admin, require('./routes/param').param_del);
app.all('/config/upd', midware_login_admin, require('./routes/param').param_upd);

//院系维护
app.all('/dept/list', midware_login_admin, require('./routes/department').dept_list);
app.all('/dept/add', midware_login_admin, require('./routes/department').dept_add);
app.all('/dept/del/:department_id', midware_login_admin, require('./routes/department').dept_del);
app.all('/dept/upd', midware_login_admin, require('./routes/department').dept_upd);
app.all('/dept/tree', midware_login_admin, require('./routes/department').tree);

//班级维护
app.all('/class/add', midware_login_admin, require('./routes/class').class_add);
app.all('/class/del/:class_id', midware_login_admin, require('./routes/class').class_del);
app.all('/class/upd', midware_login_admin, require('./routes/class').class_upd);
app.all('/class/list', midware_login_admin, require('./routes/class').class_list);

//实习地点维护
app.all('/location/list', midware_login_admin, require('./routes/location').location_list);
app.all('/location/add', midware_login_admin, require('./routes/location').location_add);
app.all('/location/del/:location_id', midware_login_admin, require('./routes/location').location_del);
app.all('/location/upd', midware_login_admin, require('./routes/location').location_upd);

//加入用户
app.all('/role/adduser',midware_login_admin,require('./routes/role').role_adduser);
//角色授权
app.all('/role/addresource',midware_login_admin,require('./routes/role').role_addresource);

//学生管理 
app.all('/student/list', midware_login_admin, require('./routes/student').student_list);
app.all('/student/add', midware_login_admin, require('./routes/student').student_add);
app.all('/student/upd', midware_login_admin, require('./routes/student').student_upd);
app.all('/student/del', midware_login_admin, require('./routes/student').student_del);
app.all('/student/search_name', midware_login_admin, require('./routes/student').student_search_name);
app.all('/student/get_admin_group', midware_login_admin, require('./routes/student').get_admin_group);
// app.all('/student/get_classes', midware_login_admin, require('./routes/student').student_get_classes);
app.all('/student/filter', midware_login_admin, require('./routes/student').student_filter);


//老师管理
app.all('/teacher/list', midware_login_admin, require('./routes/teacher').teacher_list);
app.all('/teacher/add', midware_login_admin, require('./routes/teacher').teacher_add);
app.all('/teacher/upd', midware_login_admin, require('./routes/teacher').teacher_upd);
app.all('/teacher/del', midware_login_admin, require('./routes/teacher').teacher_del);
app.all('/teacher/search_name', midware_login_admin, require('./routes/teacher').teacher_search_name);
app.all('/teacher/get_leaders', midware_login_admin, require('./routes/teacher').get_leaders);
app.all('/teacher/get_teachers', midware_login_admin, require('./routes/teacher').get_teachers);

// 微信相关
app.all('/wechat/token_verify',require('./routes/wechat').token_verify);
app.all('/wechat/access_token',require('./routes/wechat').get_access_token);
app.all('/wechat/tpl_msg_test',require('./routes/wechat').send_tpl_msg_test);
app.all('/wechat/get_signature_by_url',require('./routes/wechat').get_signature_by_url);
app.all('/wechat/user_info',require('./routes/wechat').wechat_user_info);
app.all('/wechat/oauth2/callback',require('./routes/wechat').oauth2_callback);

app.all('/test/session',require('./routes/test').test_session);
app.all('/test/session_clear',require('./routes/test').test_session_clear);

app.all("/test",midware_login_wechat,function(req,res){
  return res.render("ask_leave");
});

//学生信息相关
app.all('/user/add_user',require('./routes/user').add_user);
app.all('/user/edit_user',require('./routes/user').edit_user);
app.all('/user/fw_session',require("./routes/user").user_fw_session);
//app.all('/user/get_user',require('./routes/user').get_user);

//后台推送消息相关
app.all('/msg/send_msg',require('./routes/msg').send_msg);

// 视图相关
app.get("/views/sign",midware_login_wechat,midware_login_wechat_binduser,require("./routes/sign").views_sign);

app.get("/views/sign_calendar",midware_login_wechat,midware_login_wechat_binduser,function(req,res){
  return res.render("sign_calendar");
})

app.get("/views/ask_leave",midware_login_wechat,midware_login_wechat_binduser,function(req,res){
  return res.render("ask_leave");
})

app.get("/views/user_center",require("./routes/user").views_user_center);

app.get("/views/bind",midware_login_wechat, function(req,res){
  var wechat_user = req.session.user_fw.wx;
  return res.render("bind",{wechat_user:wechat_user});
})

app.get("/views/leave_list",midware_login_wechat,midware_login_wechat_binduser,function(req,res){
  return res.render("leave_list");
})

app.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});
